package edu.pku.sei.transformation.editor.controller.editparts.link;

import java.beans.PropertyChangeEvent;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.PolylineConnection;
import org.eclipse.swt.SWT;

import edu.pku.sei.modeler.model.structure.MetaShapeElement;
import edu.pku.sei.transformation.editor.model.ModelPackage;
import edu.pku.sei.transformation.editor.model.execution.TransformationActualParameter;
import edu.pku.sei.transformation.editor.views.figures.ConnectionFigure;

public class TransformationActualParameterEditpart extends TransformationLinkEditPart {

	public TransformationActualParameterEditpart(MetaShapeElement conn) {
		super(conn);
		// TODO Auto-generated constructor stub
	}
	
	@Override
	protected IFigure createFigure() {
		TransformationActualParameter model = (TransformationActualParameter) getLink().getModelElement();
		ConnectionFigure figure = new ConnectionFigure(SWT.LINE_DASH);
		figure.setTargetDecoration(ConnectionFigure.ARROW);
		figure.setLabel("<<Actual Parameter>>", ConnectionFigure.CONNECTION_FIGURE_MIDDLE);
		if (model.getFormalParameter() != null)
			figure.setLabel(model.getFormalParameter().toString(), ConnectionFigure.CONNECTION_FIGURE_SOURCE);
//		refreshColor();
		return figure;
	}
	
	private void refreshColor() {
		TransformationActualParameter p = (TransformationActualParameter) getLink().getModelElement();
		if (p.getData() != null
				&& p.getFormalParameter() != null
				&& p.getData().getMetamodel() == p.getFormalParameter().getMetamodel()) {
			((PolylineConnection)getFigure()).setForegroundColor(ColorConstants.black);
		} else {
			((PolylineConnection)getFigure()).setForegroundColor(ColorConstants.red);
		}
	}
	
	@Override
	protected void handlePropertyChanged(PropertyChangeEvent event) {
		String propertyName = event.getPropertyName();
		TransformationActualParameter model = (TransformationActualParameter) getLink().getModelElement();
		ConnectionFigure figure = (ConnectionFigure) getFigure();
		if (ModelPackage.TAP_TFP.equals(propertyName)) {
			if (event.getNewValue() != null)
				figure.setLabel(event.getNewValue().toString(), ConnectionFigure.CONNECTION_FIGURE_SOURCE);
		} else {
			super.handlePropertyChanged(event);
		}
	}
}
